<?php


namespace app\api\controller;

//yr.com/api/Pay/payOrder
use app\api\validate\PayValidate;

/**
 * Class Pay
 * @package app\api\controller
 * 参考代码，在任何地方继承BaseApi 直接调用processPay()
 *
 */

class Pay extends BaseApi {

    /*订单支付*/
    public function payOrder(){

    	$payValidate = new PayValidate();
	    $validateRes = $payValidate->goSceneCheck('payOrder');
	    if(is_array($validateRes) && array_key_exists('code',$validateRes)){
		    return error($validateRes['msg']);
	    }

	    $orderid = intval($this->request->param('orderid'));
	    $openid = $this->request->param('openid');

        $action = 'payResult';//回调方法
        $preData = [
           'money' => '',//查询的订单金额
           'ordersn' =>'',//查询的订单编号
           'openid' => '',//用户openid
        ];
        $body = '';//商品描述

        $item = $this->processPay($action,$preData,$body);
        return success($item);
    }

    /*微信退款*/
    public function refund(){
	    $payValidate = new PayValidate();
	    $validateRes = $payValidate->goSceneCheck('payOrder');
	    if(is_array($validateRes) && array_key_exists('code',$validateRes)){
		    return error($validateRes['msg']);
	    }

	    $orderid = intval($this->request->param('orderid'));
        $openid = $this->request->param('openid');

        //TODO 通过orderid或者openid获取申请退款订单信息 暂定为$refund
        $refund = [];

        $order = [
            'out_trade_no' => $refund['out_trade_no'],
            'out_refund_no' => time(),
            'total_fee' => $refund['fee']*100,
            'refund_fee' => $refund['fee']*100,
            'refund_desc' => '陌拼订单未拼成退款',
            'type'=>'miniapp'
        ];

        $config = $this->processConfig('payResult');
        $result = Pay::wechat($config)->refund($order);
        /*TODO 需要在这个地方更新订单为已退款，加一个属性*/
    }

}